/*
 * This file is part of Smartfood.
 * 
 * Smartfood is Copyright (C) 2012 Thibault JACQUEMIN, Vincent KURIHARA
 * 
 * Smartfood is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Smartfood is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Smartfood. If not, see <http://www.gnu.org/licenses/>.
 */
package com.smartfood;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;

import com.adapt.RecipsAdapter;
import com.data.RecipesContent;
import com.smartfood.R;

public class MyRecip extends Activity implements OnClickListener, OnMenuItemClickListener {
	private ShareData data;
	private ListView mainListView;
	private RecipsAdapter adapt;
	private RecipesContent[] recList; 
	private Button toast;
	
	private final int MYTASTESLIST=0;
	private final int AaZLIST=1;
	private final int PRICELISTASC=2;
	private final int PRICELISTDESC=3;
	private final int DIFFICULTYLISTASC=4;
	private final int DIFFICULTYLISTDESC=5;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_customlist2);
		toast=(Button)findViewById(R.id.toast);
		toast.setText(R.string.toast_myrecipes);
		toast.setOnClickListener(this);
		TextView toastLabel=(TextView)findViewById(R.id.toast_label);
		toastLabel.setText(R.string.toast_recipelabel);
		Button btnBack=(Button)findViewById(R.id.back);
		btnBack.setOnClickListener(this);
		Button btnAdd=(Button)findViewById(R.id.action);
		btnAdd.setOnClickListener(this);
		btnAdd.setText(R.string.addrecipe);
		updateList(MYTASTESLIST);
	}
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.toast:
				showPopupChoiceMode(toast);
				break;
			case R.id.back:
				finish();
				break;
	        case R.id.action:
				// show window to add a recipe
				Intent intent=new Intent(MyRecip.this,AddRecipe.class);
				startActivity(intent);
				break;
		}
	}
	void showPopupChoiceMode(View v){
		PopupMenu pop = new PopupMenu(this, v);
		pop.setOnMenuItemClickListener(this);
		pop.inflate(R.menu.menu_listviewoptions);
		pop.show();
	}
	public boolean onMenuItemClick(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.myrecipes:
	        	toast.setText(R.string.toast_myrecipes);
	        	updateList(MYTASTESLIST);
	            return true;
	        case R.id.allrecipes:
	        	toast.setText(R.string.toast_allrecipes);
	        	updateList(AaZLIST);
	        	return true;
	        case R.id.ascpricerecipes:
	        	toast.setText(R.string.toast_ascpricerecipes);
	        	updateList(PRICELISTASC);
	        	return true;
	        case R.id.descpricerecipes:
	        	toast.setText(R.string.toast_descpricerecipes);
	        	updateList(PRICELISTDESC);
	        	return true;
	        case R.id.ascdifficultyrecipes:
	        	toast.setText(R.string.toast_ascdifficultyrecipes);
	        	updateList(DIFFICULTYLISTASC);
	        	return true;
	        case R.id.descdifficultyrecipes:
	        	toast.setText(R.string.toast_descdifficultyrecipes);
	        	updateList(DIFFICULTYLISTDESC);
	        	return true;
        	default:
        		return false;
	    }
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu,v,menuInfo);
		AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)menuInfo;
		menu.setHeaderTitle(recList[info.position].getName());
		menu.add(info.position,0,0,R.string.addtomlist);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int pos=item.getGroupId();
		data.recips.add(recList[pos]);
		return super.onContextItemSelected(item); 
	}
	
	public void updateList(int recModeID){
		data=(ShareData)getApplication();
		switch (recModeID) {
			case MYTASTESLIST:
				recList=data.db.requestTasteRecipes();
				break;
			case AaZLIST:
				recList=data.db.requestRecipesList(AaZLIST);
				break;
			case PRICELISTASC:
				recList=data.db.requestRecipesList(PRICELISTASC);
				break;
			case PRICELISTDESC:
				recList=data.db.requestRecipesList(PRICELISTDESC);
				break;
			case DIFFICULTYLISTASC:
				recList=data.db.requestRecipesList(DIFFICULTYLISTASC);
				break;
			case DIFFICULTYLISTDESC:
				recList=data.db.requestRecipesList(DIFFICULTYLISTDESC);
				break;
			default:
				recList=data.db.requestTasteRecipes();
		}
		mainListView=(ListView)findViewById(R.id.list);
		adapt=new RecipsAdapter(this,R.layout.item_recip,recList);
		mainListView.setAdapter(adapt);
		mainListView.setCacheColorHint(0);
		mainListView.setItemsCanFocus(false);
		mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		registerForContextMenu(mainListView);
		mainListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a,View v,int position,long id) {
				Bundle objetbunble=new Bundle();
				objetbunble.putInt("idrecip",recList[position].getId());
				Intent intent=new Intent(MyRecip.this,Recipe.class);
				intent.putExtras(objetbunble);
				startActivity(intent);
			}
		});
	}
}